<template>
    <div v-cloak class="aui-wrapper aui-page__iframe">
        <div class="aui-content__wrapper">
            <div class="aui-content">
                <el-card shadow="never" class="aui-card--fill">
                    <div class="mod-lswms__inventory">
                        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
                            <el-row>
                                <el-col :span="2">
                                    <el-form-item>
                                        <el-input v-model="dataForm.location" size="small" placeholder="库位编码" clearable>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="2">
                                    <el-form-item>
                                        <el-input v-model="dataForm.tray" size="small" placeholder="箱" clearable>
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="2">
                                    <el-form-item>
                                        <el-button size="small" @click="getDataList()">查询</el-button>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                        <el-table v-loading="dataListLoading" :data="dataList" border
                                  @selection-change="dataListSelectionChangeHandle" @sort-change="dataListSortChangeHandle"
                                  style="width: 100%;" >
                            <el-table-column prop="tray" label="箱" header-align="center" align="center">
                            </el-table-column>
                            <el-table-column prop="location" label="库位编码" header-align="center" align="center">
                            </el-table-column>
                            <el-table-column prop="updatuser" label="更新人" header-align="center" align="center">
                            </el-table-column>
                            <el-table-column prop="updatetime" label="更新时间" header-align="center" align="center">
                            </el-table-column>
                            <el-table-column label='操作' fixed="right" header-align="center" align="center" width="150">
                                <template #default="scope">
                                    <el-button type="text" size="small" v-if="scope.row.location!='PY'" @click="createMoveTask(scope.row.tray,scope.row.location)" >移库</el-button>
                                    <el-button type="text" size="small" v-if="scope.row.location=='PY'" @click="listHandle(scope.row.tray)" >明细</el-button>
                                    <!--<el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
                                </template>
                            </el-table-column>
                        </el-table>
                        <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit"
                                       :total="total" layout="total, sizes, prev, pager, next, jumper"
                                       @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
                        </el-pagination>
                        <!-- 弹窗, 新增 / 修改 -->
                        <!--<inventoryAddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate"-->
                                              <!--@refresh-data-list="getDataList"></inventoryAddOrUpdate>-->
                        <cargodataillist v-if="cargodataillistVisible" ref="cargodataillistvue"></cargodataillist>
                    </div>
                </el-card>
            </div>
        </div>
    </div>
</template>

<script>
    import DataList from "@/utils/DataList";
    import {
        ref,
        reactive,
        onMounted,
        nextTick
    } from 'vue';
    import cargodataillist from "@/views/lswms/cargo/Cargodataillist";
    import { getCodeListByCode } from "@/network/lswms/base/Codelist.js";
    import {
        getSkucategoryById,
        getSkucategoryByType
    } from "@/network/lswms/base/Skucategory"
    import {
        ElMessage,
        ElMessageBox
    } from 'element-plus'
    import {
        putCreateMoveTask
    } from "@/network/lswms/task/Task.js"
    //import {hasPermission} from "@/utils/hasPermission"
    export default {
        name: "inventory",

        components: {
            cargodataillist
        },

        setup() {
            const {
                dataForm,
                mixinViewModuleOptions,
                getDataList,
                dataList,
                total,
                page,
                limit,
                dataListLoading,
                dataListSelectionChangeHandle,
                dataListSortChangeHandle,
                pageSizeChangeHandle,
                pageCurrentChangeHandle,
                addOrUpdateHandle,
                deleteHandle,
                exportHandle,
            } = DataList();

            onMounted(() => {
                mixinViewModuleOptions.value.getDataListIsPage = true;
                mixinViewModuleOptions.value.getDataListURL = '/lswms/inventory/cargo';
                mixinViewModuleOptions.value.deleteURL = '/lswms/inventory';
                mixinViewModuleOptions.value.exportURL = '/lswms/inventory/export';
                mixinViewModuleOptions.value.deleteIsBatchKey = 'id';
                mixinViewModuleOptions.value.deleteIsBatch = true
                if (mixinViewModuleOptions.value.activatedIsNeed) {
                    getDataList();
                }
                boxsFn()
                getSkucategoryList()
            })


            const cargodataillistVisible = ref(false);
            const cargodataillistvue = ref(null);

            //打开list页
            const listHandle = (tray) => {
                cargodataillistVisible.value = true;
                nextTick(function() {
                    cargodataillistvue.value.dataForm.tray = tray;
                    cargodataillistvue.value.init();
                })
            }

            const boxs = ref("");

            const boxsFn = () => {

                getCodeListByCode('BOXLIST').then((res) => {
                    if (res.code !== 0) {
                        return ElMessage.error(res.msg);
                    }
                    boxs.value = res.data;
                }).catch((err) => {});
            }

            const sortvalue = ref([]);
            const handleChange = (sortvalue) =>{
                dataForm.value.sort = "";
                if(!sortvalue){
                    return ;
                }
                for (let i = 0; i < sortvalue.length; i++) {
                    dataForm.value.sort += sortvalue[i] + '#';
                }
                console.log(dataForm.value.sort );
            }

            const skucategoryList = ref([]);
            const getSkucategoryList = () => {
                return getSkucategoryByType(0)
                    .then(function(res) {
                        if (res.code !== 0) {
                            return ElMessage.error(res.msg);
                        }
                        // skucategoryList.value = res.data;

                        let data1 = [];
                        createList(res.data, data1);
                        skucategoryList.value = data1;
                    })
                    .catch(function() {});
            }
            const createList = (sourcedata, targetdata) => {
                for (let i = 0; i < sourcedata.length; i++) {
                    let tempdata = sourcedata[i];
                    if (tempdata.children.length > 0) {
                        let children = {
                            categorykey: '',
                            name: '',
                            children: []
                        }
                        children.categorykey = tempdata.categorykey;
                        children.name = tempdata.name;
                        createList(tempdata.children, children.children);
                        targetdata.push(children)
                    } else {
                        let children = {
                            categorykey: '',
                            name: ''
                        }
                        children.categorykey = tempdata.categorykey;
                        children.name = tempdata.name;
                        targetdata.push(children)
                    }
                }

            }
            //移库
            const createMoveTask =(carno,carlocation)=>{

                ElMessageBox.confirm('确定要将箱移出立体库吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {

                    let para =  {

                        "fromtray": carno,
                        "fromlocation": carlocation,
                        "tolocation":"PY"

                    }
                    putCreateMoveTask(para).then(function(res) {
                        if (res.code != 0) {
                            return ElMessage.error(res.msg);
                        }
                        ElMessage({
                            message: '操作成功,'+res.msg,
                            type: 'success',
                            duration: 2000
                        });
                    }).catch(function() {});

                }).catch(function() {})

            }

            return {
                dataForm,
                mixinViewModuleOptions,
                getDataList,
                dataList,
                total,
                page,
                limit,
                dataListLoading,
                dataListSelectionChangeHandle,
                dataListSortChangeHandle,
                pageSizeChangeHandle,
                pageCurrentChangeHandle,
                addOrUpdateHandle,
                deleteHandle,
                exportHandle,
                boxs,
                skucategoryList,
                sortvalue,
                handleChange,
                createMoveTask,
                listHandle,
                cargodataillistVisible,
                cargodataillistvue
            }
        },
        // methods:{
        // 	hasPermission
        // }
    }
</script>

<style scoped  lang="scss" >

</style>
